package buaa.leetcode.String;

import java.util.HashMap;

public class RomanAndInteger {
	public static String intToRoman(int num) {
		int[] value = new int[]{1000,900,500,400,100,90,50,40,10,9,5,4,1};
		String[] symbol = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL",  
										"X", "IX", "V", "IV","I" }; 
		StringBuilder result = new StringBuilder();
		for(int i = 0;i < value.length; i++){
			while(num >= value[i]){
				result = result.append(symbol[i]);
				num = num - value[i];
			}
		}
        return result.toString();
    }
	public static int RomanToInt(String str){
		if(str.length() == 0){return 0;}
		str = str.toUpperCase();
		HashMap<Character, Integer> map= new HashMap<>();
		map.put('M', 1000);
		map.put('D', 500);
		map.put('C', 100);
		map.put('L', 50);
		map.put('X', 10);
		map.put('V', 5);
		map.put('I', 1);
		int result = map.get(str.charAt(str.length()-1));
		for(int i = str.length()-2; i >= 0 ; i--){
			if(map.get(str.charAt(i)) < map.get(str.charAt(i+1))){
				result = result - map.get(str.charAt(i));
			}else{
				result = result + map.get(str.charAt(i));
			}
		}
		return result;
	}
}
